//
//  CLUIBaseTableViewController.m
//  CityHunter
//
//  Created by chuanqixiong on 14/11/25.
//  Copyright (c) 2014年 CieNet. All rights reserved.
//

#import "CLUIBaseTableViewController.h"
#import "ConfigDefine.h"
#import "Common.h"
#import "SVProgressHUD.h"

#define kNavigationBtnSpaceHorizontal 5.0f
#define kNavigationBtnSpaceVertical 10.0f
#define kNavigationBtnHeight (self.navigationController.navigationBar.frame.size.height - 2 * kNavigationBtnSpaceVertical)

#define kTitleBarHeight 64.0
#define kBottomBarHeight                    44.0

#define kNavigationBarLeftBtnTag            1000
#define kNavigationBarRightBtnTag            1001
#define kRequestFailRetBtnTag                10000

#ifndef GET_VIEW_RIGHT
#define GET_VIEW_RIGHT(view) (view.frame.origin.x + view.frame.size.width)
#endif
#ifndef GET_VIEW_BOTTOM
#define GET_VIEW_BOTTOM(view) (view.frame.origin.y + view.frame.size.height)
#endif
#define FrameHeight 64
#define Height 44

#define CELLACCESSORYIMG @"pub_title_backbtn_rightarrow"

@interface CLUIBaseTableViewController ()

@property (nonatomic, retain) NSArray *requestList;

@property (nonatomic, retain) UIView *maskView;

 @end

@implementation CLUIBaseTableViewController

/**
 * @brief
 *
 * Detailed comments of this function
 * @param[in]
 * @param[out]
 * @return
 * @note
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [UIView new];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor whiteColor], NSForegroundColorAttributeName,
                                                                     nil]];

 }

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
 }

/**
 * @brief
 *
 * Detailed comments of this function
 * @param[in]
 * @param[out]
 * @return
 * @note
 */
- (void)viewDidUnload {
    //仅仅在IOS6之前的版本有效
    [super viewDidUnload];
 }

- (void)didReceiveMemoryWarning {
    NSLog(@"%@",[self class]);
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 * @brief
 *
 * Detailed comments of this function
 * @param[in]
 * @param[out]
 * @return
 * @note
 */
- (void)dealloc {
    NSLog(@"[%@ dealloc]",[self class]);

#ifndef COMPILE_ARC_MODE
    [super dealloc];
#endif
}

/**
 * @brief
 *
 * Detailed comments of this function
 * @param[in]
 * @param[out]
 * @return
 * @note
 */
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - navigation
//设置左、右侧文字 或者单独设置某一侧文字
- (void)showNavgationWithLeftTiele:(NSString *)title
                 andleftBtnAction:(SEL)laction
                     WithRightTiele:(NSString *)rtitle
                  andrigheBtnAction:(SEL)raction{
    if(![title isEqualToString:@""]){
        UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
        btnLeft.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
        [btnLeft setTitleEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];
        [btnLeft setTitle:title forState:UIControlStateNormal];
        [btnLeft addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
        CGFloat height = kNavigationBtnHeight;
        btnLeft.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   60,
                                   height);
        UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
        self.navigationItem.leftBarButtonItem = leftBar;
    }
    
    if (![rtitle isEqualToString:@""]) {
        UIButton *btnRighe = [UIButton buttonWithType:UIButtonTypeCustom];
        btnRighe.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
        [btnRighe setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -30)];
        [btnRighe setTitle:rtitle forState:UIControlStateNormal];
        [btnRighe addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
        CGFloat height = kNavigationBtnHeight;
        btnRighe.frame = CGRectMake(0,
                                    kNavigationBtnSpaceHorizontal,
                                    60,
                                    height);
        UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithCustomView:btnRighe];
        self.navigationItem.rightBarButtonItem = rightBar;
    }
}

- (void)showNavgationWithLeftItem{
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnLeft setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
    [btnLeft addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    UIImage *leftImg = [UIImage imageNamed:BACKIMAGE];
    btnLeft.tag = kNavigationBarLeftBtnTag;
    CGFloat height = kNavigationBtnHeight;
    CGFloat width = height * leftImg.size.width / leftImg.size.height;
    if (height/width < 0.8) {
        CGFloat scale = leftImg.size.height / leftImg.size.width;
        btnLeft.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   height,
                                   height*scale);
    } else {
        btnLeft.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   width,
                                   height);
    }
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
    self.navigationItem.leftBarButtonItem = leftBar;
}

- (void)showNavgationWithLeftItemWithTitle:(NSString *)title{
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnLeft setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
    [btnLeft addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    UIImage *leftImg = [UIImage imageNamed:BACKIMAGE];
    btnLeft.tag = kNavigationBarLeftBtnTag;

    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -10;
    
    CGFloat height = kNavigationBtnHeight;
    CGFloat width = height * leftImg.size.width / leftImg.size.height;
    if (height/width < 0.8) {
        CGFloat scale = leftImg.size.height / leftImg.size.width;
        btnLeft.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   height,
                                   height*scale);
    } else {
        btnLeft.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   width,
                                   height);
    }

    UIButton *btnTiele = [UIButton buttonWithType:UIButtonTypeCustom];
    btnTiele.titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
    btnTiele.titleLabel.textAlignment = NSTextAlignmentLeft;
    [btnTiele setTitle:title forState:UIControlStateNormal];
    [btnTiele setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnTiele.frame  = CGRectMake(CGRectGetMaxX(btnLeft.frame)-20, CGRectGetMidY(btnLeft.frame), 70, btnLeft.frame.size.height);
    [btnTiele addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    if (title.length == 2) {
        btnTiele.titleEdgeInsets = UIEdgeInsetsMake(0,-45,0,0);
    }else{
        btnTiele.titleEdgeInsets = UIEdgeInsetsMake(0,-10,0,0);
    }

    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
    UIBarButtonItem *leftTitleBar = [[UIBarButtonItem alloc] initWithCustomView:btnTiele];
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,leftBar,leftTitleBar, nil];
}

- (void)showNavBarWithRightImage:(UIImage *)rimage
               andRightBtnAction:(SEL)raction
                    LeftBtnImage:(UIImage *)limage
                andLeftBtnAction:(SEL)laction
                           title:(NSString *)title
                        barColor:(UIColor *)color {
    if (limage) {
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftBtn setBackgroundImage:limage forState:UIControlStateNormal];
        if (laction) {
            [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
        }
        leftBtn.tag = kNavigationBarLeftBtnTag;
        CGFloat height = kNavigationBtnHeight;
        CGFloat width = height * limage.size.width / limage.size.height;
        leftBtn.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   width,
                                   height);
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    }
    if (rimage) {
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setBackgroundImage:rimage forState:UIControlStateNormal];
        if (raction) {
            [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
        }
        rightBtn.tag = kNavigationBarRightBtnTag;
        CGFloat height = kNavigationBtnHeight;
        CGFloat width = height * rimage.size.width / rimage.size.height;
        rightBtn.frame = CGRectMake(0,
                                    kNavigationBtnSpaceHorizontal,
                                    width,
                                    height);
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    }
    
    if (color) {
        [self.navigationController.navigationBar setBackgroundImage:nil
                                                      forBarMetrics:UIBarMetricsDefault];
        if ([self.navigationController.navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
            self.navigationController.navigationBar.barTintColor = color;
        } else {
            self.navigationController.navigationBar.tintColor = GLOBAL_BARTINT_COLOR;
        }
    }
    [self showNavBarWithTitle:title];
}

- (void)showNavBarWithRightTitle:(NSString *)rightTitle andRightAction:(SEL)raction andLeftAction:(SEL)laction  andFontSize:(CGFloat)size andRightWidth:(CGFloat)width{
    //左边返回为箭头，右边是文字
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
    if (laction) {
        [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
        
    }else{
        [leftBtn addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
        
    }
    leftBtn.frame = CGRectMake(0,
                               kNavigationBtnSpaceHorizontal,
                               kNavigationBtnHeight * [UIImage imageNamed:BACKIMAGE].size.width / [UIImage imageNamed:BACKIMAGE].size.height,
                               kNavigationBtnHeight);
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBar;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setFrame:CGRectMake(15, kNavigationBtnSpaceHorizontal, width, 30)];
    [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
    [rightBtn.titleLabel setFont: [UIFont boldSystemFontOfSize:size]];
    UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rigthBar;
}

- (void)showNavBarWithRightImage:(UIImage *)rimage andRightBtnAction:(SEL)raction {
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setBackgroundImage:rimage forState:UIControlStateNormal];
    if (raction) {
        [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
    }
    rightBtn.tag = kNavigationBarRightBtnTag;
    CGFloat height = kNavigationBtnHeight;
    CGFloat width = height * rimage.size.width / rimage.size.height;
    rightBtn.frame = CGRectMake(0,
                                kNavigationBtnSpaceHorizontal,
                                width,
                                height);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

/**
 * @brief
 *
 * 设置Nav左右的按钮
 * @param[in]
 * @param[out]
 * @return
 * @note
 */
- (void)showNavBarWithRightTitle:(NSString *)rightTitle
                  andRightAction:(SEL)raction
                   andLeftAction:(SEL)laction
                 andShowRightBtn:(BOOL) isShow{
    //左边返回为箭头，右边是文字
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
    if (laction) {
        [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
    }else{
        [leftBtn addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    }
    leftBtn.frame = CGRectMake(0,
                               kNavigationBtnSpaceHorizontal,
                               kNavigationBtnHeight * [UIImage imageNamed:BACKIMAGE].size.width / [UIImage imageNamed:BACKIMAGE].size.height,
                               kNavigationBtnHeight);
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBar;
    if (isShow) {
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setFrame:CGRectMake(15, kNavigationBtnSpaceHorizontal, 60, 30)];
        [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
        [rightBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        self.navigationItem.rightBarButtonItem = rigthBar;
    }
    
}

/**
 * 自定义Nav右按钮(右边以图片形式且双按钮)
 * @parama 右边按钮的title
 * @parama 右边按钮的SEL
 */
- (void)showNavBarWithRightOneImage:(UIImage *)rightOneImage
                   andRightTwoImage:(UIImage *)rightTwoImage
                  andRightOneAction:(SEL)ractionOne
                   andRighTwoAction:(SEL)ractionTwo{
    
    UIButton *btnRightOne = [UIButton buttonWithType:UIButtonTypeCustom];
    UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:btnRightOne];
    [btnRightOne setBackgroundImage:rightOneImage forState:UIControlStateNormal];
    btnRightOne.adjustsImageWhenHighlighted = NO;
    btnRightOne.contentMode = UIViewContentModeScaleAspectFit;
    btnRightOne.tag = kNavigationBarLeftBtnTag;
    CGFloat heightOne = kNavigationBtnHeight;
    CGFloat widthOne = heightOne * rightOneImage.size.width / rightOneImage.size.height;
    if (heightOne/widthOne < 0.8) {
        CGFloat scale = rightOneImage.size.height / rightOneImage.size.width;
        btnRightOne.frame = CGRectMake(KScreenWidth - (KScreenWidth - heightOne - 10),
                                       kNavigationBtnSpaceHorizontal,
                                       heightOne,
                                       heightOne*scale);
    } else {
        btnRightOne.frame = CGRectMake(KScreenWidth - (KScreenWidth - widthOne ),
                                       kNavigationBtnSpaceHorizontal,
                                       widthOne,
                                       heightOne);
    }
    [btnRightOne setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnRightOne addTarget:self action:ractionOne forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btnRightTwo = [UIButton buttonWithType:UIButtonTypeCustom];
    UIBarButtonItem *rigthBarTwo = [[UIBarButtonItem alloc] initWithCustomView:btnRightTwo];
    [btnRightTwo setBackgroundImage:rightTwoImage forState:UIControlStateNormal];
    btnRightTwo.adjustsImageWhenHighlighted = NO;
    btnRightTwo.contentMode = UIViewContentModeScaleAspectFit;
    btnRightTwo.tag = kNavigationBarLeftBtnTag;
    CGFloat heightTwo = kNavigationBtnHeight;
    CGFloat widthTwo = heightTwo * rightTwoImage.size.width / rightTwoImage.size.height;
    if (heightTwo/widthTwo < 0.8) {
        CGFloat scale = rightTwoImage.size.height / rightTwoImage.size.width;
        btnRightTwo.frame = CGRectMake(KScreenWidth - (KScreenWidth - heightTwo - heightOne - 10 ),
                                       kNavigationBtnSpaceHorizontal,
                                       heightTwo,
                                       heightTwo*scale);
    } else {
        btnRightTwo.frame = CGRectMake(KScreenWidth - (KScreenWidth - widthTwo - widthOne ),
                                       kNavigationBtnSpaceHorizontal,
                                       widthTwo,
                                       heightTwo);
    }
    [btnRightTwo setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnRightTwo addTarget:self action:ractionTwo forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *negativeSpacerRight = [[UIBarButtonItem alloc]
                                            initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                            target:nil action:nil];
    negativeSpacerRight.width = -5;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacerRight,rigthBar,negativeSpacerRight,negativeSpacerRight,rigthBarTwo, nil];
    
}

- (void)showNavBarWithTitle:(NSString *)title {
    if (title) {
        self.title = title;
    }
}

 - (void)setNavBarBgImgView:(NSString *)imageStr {
    self.navigationController.navigationBarHidden = NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    if ([Common systemVersion] == ESystemVer6OrLess) {
        if (!imageStr) {
            return;
        }
    } else {
        if (!imageStr) {
            return;
        }
        self.navigationController.navigationBar.translucent = NO;
    }
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:imageStr]
                                                                 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                  forBarMetrics:UIBarMetricsDefault];
}

- (void)addTableHeaderViewOrFooterView:(BOOL)bol{
    UIView *view = [[UIView alloc] init];
    if (bol) { //头视图
        self.tableView.tableHeaderView = view;
    }else{
        self.tableView.tableFooterView = view;
    }
}

- (void)backTo
{
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)rightBtnAction {
    
}

#pragma mark - table
- (void)setAccessoryView:(UITableViewCell *)cell{
    [cell setAccessoryView: [[UIImageView alloc] initWithImage:[UIImage imageNamed:CELLACCESSORYIMG]]];
}

#pragma mark - mask

- (void)displayMask {
    if (!self.maskView) {
        self.maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        self.maskView.backgroundColor = [UIColor clearColor];
        self.maskView.alpha = 1.f;
    }
    if (!self.maskView.superview) {
        [self.view.window addSubview:self.maskView];
    }
}

- (void)hideMask {
    if (self.maskView) {
        [self.maskView removeFromSuperview];
    }
}
- (void)displayProgressHUD {
  [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
}

- (void)hideProgressHUD {
    [SVProgressHUD dismiss];
}


#pragma mark - Present
- (BOOL)isPresent {
    if (self.navigationController) {
        return self == self.navigationController.viewControllers.lastObject;
    }
    return YES;
}
@end
